package com.zaoniao.enstudy.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.zaoniao.enstudy.domain.entity.ZaoWord;
import com.zaoniao.enstudy.domain.entity.bo.ZaoWordBo;
import com.zaoniao.enstudy.domain.repository.ZaoWordRepository;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import com.zaoniao.enstudy.infrastructure.convert.ZaoWordCrt;
import com.zaoniao.enstudy.infrastructure.mapper.ZaoWordMapper;
import com.zaoniao.enstudy.infrastructure.po.ZaoWordPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 单词数据持久层实现类
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZaoWordRepositoryImpl implements ZaoWordRepository {

    private final ZaoWordMapper zaoWordMapper;

    /**
     * 根据年级随机获取10个单词信息
     *
     * @param grade 年级
     * @return 单词李彪
     */
    @Override
    public PagedGridResult<ZaoWord> getWordListRand(Integer grade, String userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ZaoWordBo> zaoWordBos = zaoWordMapper.getWordListRand(grade, userId);
        PagedGridResult<ZaoWordBo> zaoWordBoPagedGridResult = PagedGridResult.setterPagedGrid(zaoWordBos);
        return ZaoWordCrt.INSTANCE.bo2doPage(zaoWordBoPagedGridResult);
    }

    /**
     * 批量新增单词数据
     *
     * @param zaoWords 单词列表数据
     */
    @Override
    public void batchInsert(List<ZaoWord> zaoWords) {
        zaoWordMapper.insertList(ZaoWordCrt.INSTANCE.do2poList(zaoWords));
    }

    /**
     * 获取单词详情
     *
     * @param word 单词
     * @return 单词详情信息
     */
    @Override
    public ZaoWord getWordDetail(String word) {
        ZaoWordPo po = new ZaoWordPo();
        po.setWord(word);
        ZaoWordPo zaoWordPo = zaoWordMapper.selectOne(po);
        return ZaoWordCrt.INSTANCE.po2do(zaoWordPo);
    }

    /**
     * 获取所有单词
     *
     * @return 单词列表
     */
    @Override
    public List<ZaoWord> getAll() {
        List<ZaoWordPo> zaoWordPos = zaoWordMapper.selectAll();
        return ZaoWordCrt.INSTANCE.po2doList(zaoWordPos);
    }

}
